import os


class FolderManager(object):
    """
    文件夹管理
    """

    @staticmethod
    def mkdir(path):
        """
        创建文件夹
        :param path:
        :return:
        """
        is_exists = os.path.exists(path)
        if not is_exists:
            os.mkdir(path)

    # @staticmethod
    def get_file_list(self, dir_path="", file_ext="all"):
        """
        获取文件夹中指定的格式的文件
        :param dir_path:
        :param file_ext:
        :return:
        """
        file_list = []
        for file_name in os.listdir(dir_path):
            file_path = os.path.join(dir_path, file_name)
            if os.path.isfile(file_path):
                if file_ext == ".doc" or file_ext == ".docx":
                    if os.path.splitext(file_path)[1] in ['.doc', ".docx"]:
                        file_list.append(file_path)
                elif file_ext == "all":
                    file_list.append(file_path)
                elif os.path.splitext(file_path)[1] == file_ext:
                    file_list.append(file_path)
            if os.path.isdir(file_path):
                dir_file_list = self.get_file_list(file_path, file_ext)
                file_list += dir_file_list
        # file_list.sort()
        return file_list

    @staticmethod
    def get_dir_info(dir_path="", file_ext="all"):
        """
        获取文件夹中指定的文件夹
        :param dir_path:
        :param file_ext:
        :return:
        """
        file_list = []
        for file_name in os.listdir(dir_path):
            file_path = os.path.join(dir_path, file_name)
            if os.path.isdir(file_path):
                if file_ext == "all":
                    file_list.append(file_path)
        return file_list

    def get_dir_size(self, dir_path):
        """
        获取文件夹大小
        :param dir_path:
        :return:
        """
        file_size = 0
        for file_name in os.listdir(dir_path):
            file_path = os.path.join(dir_path, file_name)
            if os.path.isdir(file_path):
                file_size += self.get_dir_size(file_path)
            elif os.path.isfile(file_path):
                file_size += os.path.getsize(file_path)
            else:
                print(f"=============={file_path} ===========")
        print(f"{dir_path} size : {round(file_size / 1000 / 1000 / 1000, 2)} G")
        return file_size
        # path_size = os.stat(dir_path)
        # print(path_size.st_size)


if __name__ == '__main__':
    folder_manager = FolderManager()
    folder_manager.get_dir_size("/home/user")
